package com.qezhhnjy.cloud.zk.curator.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhaoyangfu - 2020/8/19 16:01
 */
@Data
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "zookeeper")
public class ZkConfig {
    private String  address;
    private Integer timeout;

    @Bean
    public CuratorFramework zkClient() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(address)
                .sessionTimeoutMs(timeout)
                .retryPolicy(retryPolicy)
                .namespace("zk-curator")
                .build();
        client.start();
        return client;
    }
}
